package com.shxy.rlzy.controller;


import cn.hutool.json.JSONObject;
import com.shxy.rlzy.common.lang.Result;
import com.shxy.rlzy.entity.Depart;
import com.shxy.rlzy.entity.SalaryTaskDepart;
import com.shxy.rlzy.service.IDepartService;
import com.shxy.rlzy.service.ISalaryItemService;
import com.shxy.rlzy.service.ISalaryTaskDepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
@RestController
@RequestMapping("/salary_task_depart")
public class SalaryTaskDepartController extends BaseController {
    @Autowired
    private IDepartService departService;
    @Autowired
    private ISalaryTaskDepartService salaryTaskDepartService;
    @Autowired
    private ISalaryItemService salaryItemService;

    /**
     * 获取部门列表
     * @return
     */
    @GetMapping("/departs")
    public Result getDeparts() {
        List<Depart> departs = departService.getSalaryDeparts();
        List<JSONObject> list = departs.stream().map(depart -> convertDepart(depart)).collect(Collectors.toList());
        return Result.succ(list);
    }

    /**
     * 任务下部门列表
     * @return
     */
    @GetMapping("/task/departs/{taskId}")
    public Result getTaskDeparts(@PathVariable Long taskId) {
        List<SalaryTaskDepart> departs = salaryTaskDepartService.getDepartsByTaskId(taskId);
        return Result.succ(departs);
    }

    /**
     * 结束部门任务
     * @param id
     * @return
     */
    @PutMapping("/finish/{id}")
    public Result finishTaskById(@PathVariable Long id) {
        SalaryTaskDepart std = salaryTaskDepartService.getById(id);
        System.out.println(std.getStatu());
        if(std.getStatu() == 3) {
            return Result.fail("部门任务已完成");
        }
        salaryTaskDepartService.finishTaskById(id);
        return Result.succ("结束成功");
    }

    /**
     * 查询部门任务状态信息
     * @param taskId
     * @return
     */
    @GetMapping("/departs/status/{taskId}")
    public Result getDepartsStatus(@PathVariable Long taskId) {
        List<SalaryTaskDepart> departs = salaryTaskDepartService.getDepartsByTaskId(taskId);
        List<JSONObject> list = departs.stream().map(std -> {
            // 获取部门下员工任务数量
            int n = salaryItemService.countByDepartTaskId(std.getId());
            JSONObject obj = new JSONObject();
            obj.set("id", std.getId());
            obj.set("departId", std.getDepartId());
            obj.set("departName", std.getDepartName());
            obj.set("itemCount", n);
            obj.set("statu", std.getStatu());
            obj.set("bonus", std.getBonus());
            return obj;
        }).collect(Collectors.toList());
        return Result.succ(list);
    }

    /**
     * 重置部门任务
     * @param departTaskId
     * @return
     */
    @PutMapping("/depart/reset/{departTaskId}")
    @PreAuthorize("hasAnyAuthority('wage:task:depart:reset')")
    public Result resetById(@PathVariable Long departTaskId) {
        if(salaryTaskDepartService.resetById(departTaskId)){
            return Result.succ("重置成功");
        }
        return Result.fail("重置失败");
    }

    /**
     * 部门信息格式转换
     * @param depart
     * @return
     */
    private JSONObject convertDepart(Depart depart) {
        JSONObject obj = new JSONObject();
        obj.set("id", depart.getId());
        obj.set("name", depart.getName());
        obj.set("bonus", 0);
        return obj;
    }
}
